package com.yuexunit.operationman.nbsy.module.SafeOperation.ui;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.SafeOperation.SafeOperationContracts;
import com.yuexunit.operationman.nbsy.module.SafeOperation.bean.SafeOperationBean;
import com.yuexunit.operationman.nbsy.module.SafeOperation.model.impl.SafeOperationDetailModel;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.ISafeOperationDetailPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.impl.SafeOperationDetailPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.ui.view.ISafeOperationDetailView;
import com.yuexunit.operationman.nbsy.util.ProUtil;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;

import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2017/3/10.
 */
public class Act_SafeOperationMain extends BaseKJActivity implements ISafeOperationDetailView {

    @Bind(R.id.titlebar_tv_title)
    TextView titlebarTvTitle;
    @Bind(R.id.safeoperationmain_pic)
    ImageView safeoperationmainPic;
    @Bind(R.id.safeoperationmain_name)
    TextView safeoperationmainName;
    @Bind(R.id.safeoperationmain_age)
    TextView safeoperationmainAge;
    @Bind(R.id.safeoperationmain_sex)
    TextView safeoperationmainSex;
    @Bind(R.id.safeoperationmain_date)
    TextView safeoperationmainDate;
    @Bind(R.id.safeoperationmain_case)
    TextView safeoperationmainCase;
    @Bind(R.id.safeoperationmain_doc)
    TextView safeoperationmainDoc;
    @Bind(R.id.safeoperationmain_way)
    TextView safeoperationmainWay;
    @Bind(R.id.safeoperationmain_bed)
    TextView safeoperationmainBed;
    @Bind(R.id.safeoperationmain_visitno)
    TextView safeoperationmainVisitno;
    @Bind(R.id.safeoperationmain_operation)
    TextView safeoperationmainOperation;
    @Bind(R.id.safeoperationmain_operation_name)
    TextView safeoperationmainOperationName;

    ISafeOperationDetailPresenter presenter;

    LoadingDialog loadingDialog;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_safeoperationmain);
        ButterKnife.bind(this);

    }

    @Override
    public void initData() {
        super.initData();
        setTitle();
        loadingDialog = new LoadingDialog(this);
        presenter = new SafeOperationDetailPresenter(this, new SafeOperationDetailModel());
    }

    @Override
    public void onResume() {
        super.onResume();
        Map<String, String> params = new HashMap<>();
        params.put("operationNo", PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_OPERATION_ID) + "");
        presenter.getSafeOperationDate(params);
    }


    private void setTitle() {
        titlebarTvTitle.setText("手术安全核查表");
    }

    @OnClick({R.id.titlebar_img_back, R.id.safeoperationmain_ll_01, R.id.safeoperationmain_ll_02, R.id.safeoperationmain_ll_03, R.id.safeoperationmain_ll_04, R.id.safeoperationmain_ll_05})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_img_back:
                finish();
                break;
            case R.id.safeoperationmain_ll_01:
                startActivity(new Intent(aty, Act_SafeOperation_BeforeNarcosis.class));
                break;
            case R.id.safeoperationmain_ll_02:
                startActivity(new Intent(aty, Act_SafeOperation_BeforeOperation.class));
                break;
            case R.id.safeoperationmain_ll_03:
                startActivity(new Intent(aty, Act_SafeOperation_BeforeLeave.class));
                break;
            case R.id.safeoperationmain_ll_04:
                startActivity(new Intent(aty, Act_SafeOperation_Preview.class));
                break;
            case R.id.safeoperationmain_ll_05:
                startActivity(new Intent(aty, Act_SafeOperation_Sign.class));
                break;
        }
    }

    @Override
    public void getSafeOperationDate(SafeOperationBean safeOperationBean) {
        if (AppConfig.isEye&&null==safeOperationBean.getObject().getSecurity().getSecurityId()) {
            String safe = ProUtil.getJson("SafeOperation.json",aty);
            SafeOperationBean safeOperationBean1= JSON.parseObject(safe,SafeOperationBean.class);
            if (safeOperationBean.getCode() == 200) {
                safeOperationBean1.getObject().setSecurity(safeOperationBean.getObject().getSecurity());
                safeOperationBean1.getObject().getSecurity().setAnesthesia("局麻");
                AppConfig.SAFEOPERATIONBEAN = safeOperationBean1;

                safeoperationmainName.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getPatientName());
                safeoperationmainAge.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getPatientAge() + "岁");
                if (AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getPatientSex().equals("1")) {
                    safeoperationmainSex.setText("男");
                } else if (AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getPatientSex().equals("2")) {
                    safeoperationmainSex.setText("女");
                } else {
                    safeoperationmainSex.setText("不详");
                }
                safeoperationmainDate.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getOperationDate());
                safeoperationmainCase.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getInHospitalNo());
                safeoperationmainDoc.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getOperator());
                safeoperationmainWay.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getAnesthesia());
                safeoperationmainBed.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getBedNo());
                safeoperationmainVisitno.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getInHospitalNo());
                safeoperationmainOperation.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getOperandi());
                safeoperationmainOperationName.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getOperandi());
                if (AppConfig.SAFEOPERATIONBEAN.getObject().getSecurityItemList() == null) {
                    AppConfig.SAFEOPERATIONBEAN.getObject().setSecurityItemList(SafeOperationContracts.securityItemListEntities_new(this));
                }
            }
        } else {
            if (safeOperationBean.getCode() == 200) {
                AppConfig.SAFEOPERATIONBEAN = safeOperationBean;
                safeoperationmainName.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getPatientName());
                safeoperationmainAge.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getPatientAge() + "岁");
                if (AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getPatientSex().equals("1")) {
                    safeoperationmainSex.setText("男");
                } else if (AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getPatientSex().equals("2")) {
                    safeoperationmainSex.setText("女");
                } else {
                    safeoperationmainSex.setText("不详");
                }
                safeoperationmainDate.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getOperationDate());
                safeoperationmainCase.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getInHospitalNo());
                safeoperationmainDoc.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getOperator());
                safeoperationmainWay.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getAnesthesia());
                safeoperationmainBed.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getBedNo());
                safeoperationmainVisitno.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getInHospitalNo());
                safeoperationmainOperation.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getOperandi());
                safeoperationmainOperationName.setText(AppConfig.SAFEOPERATIONBEAN.getObject().getSecurity().getOperandi());
                if (AppConfig.SAFEOPERATIONBEAN.getObject().getSecurityItemList() == null) {
                    AppConfig.SAFEOPERATIONBEAN.getObject().setSecurityItemList(SafeOperationContracts.securityItemListEntities_new(this));
                }
            }
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }


}
